package com.android.hw.projectmenu;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.BitSet;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.util.Log;
import android.os.SystemProperties;


public class TempCtrl extends Activity {
final static String TAG="ProjectMenu/TempCtrl";

final static String DIALOG_PROP="persist.sys.temp_dialog";
final static String TEMP_PROTECT_PROP="persist.sys.hy_temp_dis";
final static String TEMP_PROTECT_FILE="/sys/class/power_supply/battery/hy_temp_dis";

/* < 0074001 xuyinwen 20151015 begin */
final static String TEMP_PROTECT_ENABLED="0";//开启温度保护
final static String TEMP_PROTECT_DISABLED="1";//关闭温度保护
/* 0074001 xuyinwen 20151015 end > */
final static String DIALOG_ENABLED="0"; //允许弹框
final static String DIALOG_DISABLED="1";//不允许弹框
private RadioGroup group;
private boolean isProtectEnabled = false;

private static byte[][] temp_protect={
	{'0'},
	{'1'}
};


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.temp_ctrl);
		
		group = (RadioGroup) this.findViewById(R.id.radioGroup1);
		group.setOnCheckedChangeListener(checkListener);
		updateView();
	}

	
	
	private OnCheckedChangeListener checkListener 
		= new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				// TODO Auto-generated method stub
	
				switch(arg1){
				case R.id.enable_temp_protect:
					setTempProtect(true);
					break;
				case R.id.disable_temp_protect:
					setTempProtect(false);
					break;					
				default:;
				}
			}
	};

	
	private void checkEnable(){
  
		
	}
	private void updateView(){
	  	String enable= SystemProperties.get(TEMP_PROTECT_PROP, TEMP_PROTECT_DISABLED);
    	if(enable.equals(TEMP_PROTECT_ENABLED)){
    		isProtectEnabled =true;
    		group.check(R.id.enable_temp_protect);
    		SystemProperties.set(DIALOG_PROP, DIALOG_ENABLED);
    	}else{
    		isProtectEnabled =false;
    		group.check(R.id.disable_temp_protect);
    		SystemProperties.set(DIALOG_PROP, DIALOG_DISABLED);
    	}
    	Log.d(TAG,"getTempProtect : "+enable);
	}
	

	
	private void setTempProtect(boolean enable){
		Log.d(TAG,"setTempProtect : "+enable);
        FileOutputStream ctrl=null;
        byte[] data;
        if(enable){ //开启温度保护->允许
        	data=temp_protect[1];
        	SystemProperties.set(TEMP_PROTECT_PROP, TEMP_PROTECT_ENABLED);
        	SystemProperties.set(DIALOG_PROP, DIALOG_ENABLED);
        }else{
        	data=temp_protect[0];
        	SystemProperties.set(TEMP_PROTECT_PROP, TEMP_PROTECT_DISABLED);
        	SystemProperties.set(DIALOG_PROP, DIALOG_DISABLED);
        }
        		
        try {
			ctrl = new FileOutputStream(TEMP_PROTECT_FILE);
			ctrl.write(data);
			ctrl.close();
        } catch (IOException e) {
        	e.printStackTrace();
        	Log.e(TAG,"setTempProtect : write failed");
        }
        
        updateView();
	}
	

}
